package com.dycx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.system.service.OssFileService;
import com.dycx.system.service.impl.OssFileServiceImpl;
import com.dycx.user.converter.VehicleConverter;
import com.dycx.user.dto.VehicleDTO;
import com.dycx.user.entity.VehicleDO;
import com.dycx.user.service.VehicleBrandService;
import com.dycx.user.service.VehicleService;
import com.dycx.user.service.VehicleTypeService;
import com.dycx.user.vo.VehicleBrandVO;
import com.dycx.user.vo.VehicleTypeVO;
import com.dycx.user.vo.VehicleVO;
import com.dycx.user.mapper.VehicleMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车辆表(Vehicle)表服务实现类
 *
 * @author wuzongbin
 * @since 2024-12-31 17:36:26
 */

@Service
@AllArgsConstructor
public class VehicleImpl extends ServiceImpl<VehicleMapper, VehicleDO> implements VehicleService {

    private VehicleMapper vehicleMapper;
    private VehicleTypeService vehicleTypeService;
    private VehicleBrandService vehicleBrandService;
    private OssFileService ossFileService;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<VehicleVO> getPage(VehicleDTO dto) {
        LambdaQueryWrapper<VehicleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getVehicleTypeId()), VehicleDO::getVehicleTypeId, dto.getVehicleTypeId());
        wrapper.like(ObjectUtil.isNotNull(dto.getLicensePlateNumber()), VehicleDO::getLicensePlateNumber, dto.getLicensePlateNumber());
        wrapper.orderByDesc(VehicleDO::getId);

        Page<VehicleDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<VehicleVO> voPage = VehicleConverter.entityToVoPage(entityPage);
        voPage.getRecords().forEach(item -> {
            if (ObjectUtil.isNotEmpty(item.getVehicleTypeId())) {
                VehicleTypeVO vo = vehicleTypeService.getInfoById(item.getVehicleTypeId());
                item.setVehicleTypeName(vo.getVehicleTypeName());
            }
            if (ObjectUtil.isNotEmpty(item.getBrandId())) {
                VehicleBrandVO vehicleBrandVO = vehicleBrandService.load(item.getBrandId());
                item.setBrandName(vehicleBrandVO.getBrandName());
            }

        });
        return voPage;
    }

    /**
     * 查询列表
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public List<VehicleVO> getList(VehicleDTO dto) {
        LambdaQueryWrapper<VehicleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getStatus()), VehicleDO::getStatus, dto.getStatus());
        wrapper.orderByDesc(VehicleDO::getId);

        List<VehicleDO> entityList = list(wrapper);

        return VehicleConverter.entityToVoList(entityList);
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public VehicleVO load(Integer id) {
        VehicleDO entity = getById(id);

        return VehicleConverter.entityToVo(entity);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(VehicleDTO dto) {
        VehicleDO entity = BeanUtil.toBean(dto, VehicleDO.class);
        if (!save(entity)) throw new RuntimeException("保存失败");
        ossFileService.setUse(dto.getDrivingLicenseImage());
        ossFileService.setUse(dto.getVehicleImage());

        return true;
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(VehicleDTO dto) {
        VehicleVO vehicleVO = load(dto.getId());
        ossFileService.setDeleted(vehicleVO.getVehicleImage());
        ossFileService.setDeleted(vehicleVO.getDrivingLicenseImage());

        VehicleDO entity = BeanUtil.toBean(dto, VehicleDO.class);
        if (!updateById(entity)) throw new RuntimeException("更新失败！");

        ossFileService.setUse(dto.getVehicleImage());
        ossFileService.setUse(dto.getDrivingLicenseImage());

        return true;
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        VehicleVO vehicleVO = load(id);
        ossFileService.setDeleted(vehicleVO.getVehicleImage());
        ossFileService.setDeleted(vehicleVO.getDrivingLicenseImage());
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return vehicleMapper.getMaxSort();
    }
}

